// #include "crow_all.h"
// #include "json.hpp"
// #include <random>
// #include <vector>

// using json = nlohmann::json;


//简易抽奖1.0

// int main() {
//     crow::SimpleApp app;

//     // 奖品池
//     const std::vector<std::string> prizes = {
//         "一等奖 MacBook", "二等奖 iPad", "三等奖 AirPods", "谢谢参与"
//     };

//     // 随机数引擎
//     std::random_device rd;
//     std::mt19937 gen(rd());
//     std::uniform_int_distribution<> dis(0, prizes.size() - 1);

//     // 抽奖接口：GET /draw
//     CROW_ROUTE(app, "/draw")([&]() {
//         crow::json::wvalue res;
//         res["prize"] = prizes[dis(gen)];
//         return res;
//     });

//     // 可选：根路径 / 做个跳转或说明
//     // CROW_ROUTE(app, "/")([]() {
//     //     return "访问 /draw 进行抽奖";
//     // });

//     CROW_ROUTE(app, "/")([]() {
//     crow::response res;
//     res.set_header("Content-Type", "text/plain; charset=utf-8");
//     res.write("访问 /draw 进行抽奖");
//     return res;
// });

//     app.port(8080).multithreaded().run();
//     return 0;
// }


//图片+音效 ，抽奖2.0 

#include "crow_all.h"
#include <random>
#include <vector>
#include <fstream>

int main() {
    crow::SimpleApp app;

    /* ---------- 1. 奖品 ---------- */
    const std::vector<std::string> prizes = {
        "一等奖 MacBook", "二等奖 iPad", "三等奖 AirPods", "谢谢参与"
    };
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, prizes.size() - 1);


/* ---------- 抽奖接口 ---------- */
CROW_ROUTE(app, "/draw")([&](const crow::request& /*req*/) {
    int idx   = dis(gen);
    std::string prize = prizes[idx];

    /* 映射图片与音效 */
    std::string img, sound;
    switch (idx) {
        case 0: img = "/files/one.jpg";   sound = "/files/first.mp3";  break;
        case 1: img = "/files/two.jpg";   sound = "/files/second.mp3"; break;
        case 2: img = "/files/three.jpg"; sound = "/files/third.mp3";  break;
        default: img = "/files/Thank_you_for_participating.jpg"; sound = "/files/false.mp3"; break;
    }

    std::ostringstream html;
    html <<
        "<!DOCTYPE html><html><head>"
        "<meta charset=\"utf-8\">"
        "<title>抽奖结果</title>"
        "<style>body{text-align:center;font-family:sans-serif}</style>"
        "</head><body>"
        "<h1>" << prize << "</h1>"
        "<img src=\"" << img << "\" width=\"300\">"
        "<audio id=\"snd\" src=\"" << sound << "\"></audio>"
        "<br>"
        "<button onclick=\"document.getElementById('snd').play();location.href='/draw';\">再来一次</button>"
        "</body></html>";

    crow::response res(html.str());
    res.set_header("Content-Type", "text/html; charset=utf-8");
    return res;
});



    /* ---------- 3. 旧版静态文件路由 ---------- */
    CROW_ROUTE(app, "/files/<path>")([&](const std::string& path) {
        std::string full = "files/" + path;

        /* 简单判断文件是否存在 */
        std::ifstream f(full, std::ios::binary);
        if (!f) {
            crow::response res(404, "404 Not Found");
            return res;
        }

        /* 读文件内容并返回 */
        std::ostringstream buf;
        buf << f.rdbuf();
        crow::response res(buf.str());

        /* 根据后缀简单设置 MIME（可选） */
        if (path.rfind(".jpg") != std::string::npos || path.rfind(".jpeg") != std::string::npos)
            res.set_header("Content-Type", "image/jpeg");
        else if (path.rfind(".png") != std::string::npos)
            res.set_header("Content-Type", "image/png");
        else if (path.rfind(".mp3") != std::string::npos)
            res.set_header("Content-Type", "audio/mpeg");
        else
            res.set_header("Content-Type", "application/octet-stream");

        return res;
    });

    /* ---------- 4. 根路径重定向 ---------- */
    CROW_ROUTE(app, "/")([]() {
        crow::response res;
        res.set_header("Location", "/draw");
        res.code = 302;
        return res;
    });

    app.port(8080).multithreaded().run();
    return 0;
}